package com.example.multithread.no1114;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

public class SemaphoreFoo {

    public SemaphoreFoo() {

    }

    Semaphore two = new Semaphore(0);
    Semaphore three = new Semaphore(0);

    public static void main(String[] args) {
        SemaphoreFoo foo = new SemaphoreFoo();

        new Thread(() -> {
            foo.first();
        }).start();
        new Thread(() -> {
            try {
                foo.second();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                foo.third();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void first() {

        System.out.print("first");
        two.release();
    }

    public void second() throws InterruptedException {
        two.acquire();
        System.out.print("two");
        three.release();
    }

    public void third() throws InterruptedException {
        three.acquire();
        System.out.print("three");
    }
}
